• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /macosx-10.10.1/pyobjc-45/2.6/pyobjc/pyobjc-framework-Quartz/Examples/Programming with Quartz/BasicDrawing/
1from Quartz import *
2
3import Utilities
4import UIHandling
5import Images
6import ImageMasking
7import BitmapContext
8import EPSPrinting
9import CoordinateSystem
10import PathDrawing
11import ColorAndGState
12import QuartzTextDrawing
13import PatternDrawing
14import ShadowsAndTransparencyLayers
15import Shadings
16import DrawingBasics
17
18
19# Defines
20kCatPDF		 = "Kitty.pdf"
21kPDFForBlendMode = "blendmode.pdf"
22
23kOurJPEG	  = "Poot.jpg"
24kQTImage	  = "ptlobos.tif"
25kOurSubstituteJPG = "LyingOnDeckNoProfile.JPG"
26kOurEPS		  = "imageturkey.eps"
27
28RAW_IMAGE_WIDTH	  = 400
29RAW_IMAGE_HEIGHT  = 300
30kRawColorImage	  = "image-400x300x24.raw"
31kOtherColorImage  = "otherimage-400x300x24.raw"
32
33MASKING_IMAGE_WIDTH  = 400
34MASKING_IMAGE_HEIGHT = 259
35kMaskingImage        = "400x259x8.bw.raw"
36
37noOffScreen     = 0
38bitmapOffScreen = 1
39layerOffScreen  = 2
40
41# Cache info for GetURL
42_mainBundle = None
43_urlMap = {}
44
45def GetURL(name):
46    """
47    Returns the CFURLRef for an embeded resource, or None of that cannot be found.
48    """
49
50    global _mainBundle
51    if _mainBundle is None:
52        _mainBundle = Utilities.getAppBundle()
53
54    mainBundle = _mainBundle
55    if mainBundle is not None:
56        if name in _urlMap:
57            return _urlMap[name]
58
59        url = CFBundleCopyResourceURL(mainBundle, name, None, None)
60        _urlMap[name] = url
61    else:
62        print >>sys.stderr, "Can't get the app bundle!"
63        return
64
65    if url is None:
66        print >>sys.stderr, "Couldn't get URL for %r"%(name,)
67
68    return url
69
70#
71# Helper functions for drawing
72#
73
74def callPDFDrawProc(context, proc, pdfFile):
75    ourPDFurl = GetURL(pdfFile)
76
77    if ourPDFurl:
78        proc(context, ourPDFurl)
79
80def doDrawJPEGFile(context):
81    ourJPEGurl = GetURL(kOurJPEG)
82
83    if ourJPEGurl is not None:
84        Images.drawJPEGImage(context, ourJPEGurl)
85
86
87def doRawImageFileWithURL(context):
88    url = GetURL(kRawColorImage)
89
90    if url is not None:
91        Images.drawImageFromURL(context, url,
92            RAW_IMAGE_WIDTH, RAW_IMAGE_HEIGHT,
93            8, True);   # 8 bits per component, isColor = True
94
95
96def doRawImageFileWithCallbacks(context):
97    url = GetURL(kRawColorImage)
98
99    if url is not None:
100        Images.doImageWithCallbacksCreatedFromURL(context, url,
101                RAW_IMAGE_WIDTH, RAW_IMAGE_HEIGHT,
102                8, True);   # 8 bits per component, isColor = True
103
104
105def doDrawImageWithCGImageSource(context):
106    url = GetURL(kOurJPEG)
107    if url is not None:
108        Images.drawImageWithCGImageDataSource(context, url)
109
110def doIncrementalImage(context):
111    url = GetURL(kOurJPEG)
112
113    if url is not None:
114        Images.doIncrementalImageWithURL(context, url)
115
116def doQTImage(context):
117    url = GetURL(kQTImage)
118
119    if url is not None:
120        Images.drawQTImageWithQuartz(context, url)
121
122
123def doJPEGDocumentWithMultipleProfiles(context):
124    url = GetURL(kOurSubstituteJPG)
125
126    if url is not None:
127        Images.drawJPEGDocumentWithMultipleProfiles(context, url)
128
129def doMaskImageWithMask(context):
130    theImageToMaskURL = GetURL(kOtherColorImage)
131    theMaskingImageURL = GetURL(kMaskingImage)
132
133    if theImageToMaskURL is not None and  theMaskingImageURL is not None:
134        ImageMasking.doMaskImageWithMaskFromURL(context, theImageToMaskURL, RAW_IMAGE_WIDTH,
135                            RAW_IMAGE_HEIGHT, 8, theMaskingImageURL, MASKING_IMAGE_WIDTH,
136                            MASKING_IMAGE_HEIGHT)
137
138def doMaskImageWithGrayImage(context):
139    theImageToMaskURL = GetURL(kOtherColorImage)
140    theMaskingImageURL = GetURL(kMaskingImage)
141
142    if theImageToMaskURL is not None and theMaskingImageURL is not None:
143        ImageMasking.doMaskImageWithGrayImageFromURL(context, theImageToMaskURL, RAW_IMAGE_WIDTH,
144                            RAW_IMAGE_HEIGHT, 8, theMaskingImageURL, MASKING_IMAGE_WIDTH,
145                            MASKING_IMAGE_HEIGHT)
146
147def doImageMaskedWithColor(context):
148    url = GetURL(kOtherColorImage)
149
150    if url is not None:
151        ImageMasking.doMaskImageWithColorFromURL(context, url,
152                        RAW_IMAGE_WIDTH, RAW_IMAGE_HEIGHT,
153                        True)
154
155def exportImageMaskedWithImage(context):
156    theImageToMaskURL = GetURL(kOtherColorImage)
157    theMaskingImageURL = GetURL(kMaskingImage)
158
159    if theImageToMaskURL is not None and theMaskingImageURL is not None:
160        ImageMasking.exportImageWithMaskFromURLWithDestination(
161                context, theImageToMaskURL, RAW_IMAGE_WIDTH,
162                RAW_IMAGE_HEIGHT, 8, theMaskingImageURL,
163                MASKING_IMAGE_WIDTH, MASKING_IMAGE_HEIGHT)
164
165def doClipMask(context):
166    theMaskingImageURL = GetURL(kMaskingImage)
167
168    if theMaskingImageURL is not None:
169            ImageMasking.drawWithClippingMask(context,
170                theMaskingImageURL, MASKING_IMAGE_WIDTH, MASKING_IMAGE_HEIGHT)
171
172def tilePDFDocument(context, offscreenType):
173    url = GetURL(kCatPDF)
174
175    if url is not None:
176        if offscreenType == noOffScreen:
177            BitmapContext.TilePDFNoBuffer(context, url)
178        elif offscreenType == bitmapOffScreen:
179            BitmapContext.TilePDFWithOffscreenBitmap(context, url)
180        else:
181            BitmapContext.TilePDFWithCGLayer(context, url)
182
183def doCompatibleEPSDrawing(context):
184    ourEPSurl = GetURL(kOurEPS)
185
186    if ourEPSurl is not None:
187        EPSPrinting.drawEPSDataImage(context, ourEPSurl)
188
189
190def DispatchDrawing(context, drawingType):
191    """ Drawing dispatcher """
192    if drawingType == UIHandling.kHICommandSimpleRect:
193            DrawingBasics.doSimpleRect(context)
194
195    elif drawingType == UIHandling.kHICommandStrokedRect:
196            DrawingBasics.doStrokedRect(context)
197
198    elif drawingType == UIHandling.kHICommandStrokedAndFilledRect:
199            DrawingBasics.doStrokedAndFilledRect(context)
200
201    elif drawingType == UIHandling.kHICommandPathRects:
202            DrawingBasics.doPathRects(context)
203
204    elif drawingType == UIHandling.kHICommandAlphaRects:
205            DrawingBasics.doAlphaRects(context)
206
207    elif drawingType == UIHandling.kHICommandDashed:
208            DrawingBasics.doDashedLines(context)
209
210    elif drawingType == UIHandling.kHICommandSimpleClip:
211            DrawingBasics.doClippedCircle(context)
212
213    elif drawingType == UIHandling.kHICommandPDFDoc:
214            callPDFDrawProc(context, DrawingBasics.doPDFDocument, kCatPDF)
215
216    elif drawingType == UIHandling.kHICommandRotatedEllipses:
217            CoordinateSystem.doRotatedEllipses(context)
218
219    elif drawingType == UIHandling.kHICommandDrawSkewCoordinates:
220            CoordinateSystem.drawSkewedCoordinateSystem(context)
221
222    elif drawingType == UIHandling.kHICommandBezierEgg:
223            PathDrawing.doEgg(context)
224
225    elif drawingType == UIHandling.kHICommandRoundedRects:
226            PathDrawing.doRoundedRects(context)
227
228    elif drawingType == UIHandling.kHICommandStrokeWithCTM:
229            PathDrawing.doStrokeWithCTM(context)
230
231    elif drawingType == UIHandling.kHICommandRotatedEllipsesWithCGPath:
232            PathDrawing.doRotatedEllipsesWithCGPath(context)
233
234    elif drawingType == UIHandling.kHICommandPixelAligned:
235            PathDrawing.doPixelAlignedFillAndStroke(context)
236
237    elif drawingType == UIHandling.kHICommandDeviceFillAndStrokeColor:
238            ColorAndGState.doColorSpaceFillAndStroke(context)
239
240    elif drawingType == UIHandling.kHICommandCLUTDrawGraphics:
241            ColorAndGState.doIndexedColorDrawGraphics(context)
242
243    elif drawingType == UIHandling.kHICommandDrawWithGlobalAlpha:
244            ColorAndGState.drawWithGlobalAlpha(context)
245
246    elif drawingType == UIHandling.kHICommandDrawWithBlendMode:
247            callPDFDrawProc(context, ColorAndGState.drawWithColorBlendMode, kPDFForBlendMode)
248
249    elif drawingType == UIHandling.kHICommandDrawWithColorRefs:
250            ColorAndGState.drawWithColorRefs(context)
251
252    elif drawingType == UIHandling.kHICommandFunctionsHaveOwnGSave:
253            ColorAndGState.doClippedEllipse(context)
254
255    elif drawingType == UIHandling.kHICommandDrawJPEGImage:
256            doDrawJPEGFile(context)
257
258    elif drawingType == UIHandling.kHICommandColorImageFromFile:
259            doRawImageFileWithURL(context)
260
261    elif drawingType == UIHandling.kHICommandColorImageFromData:
262            Images.doColorRampImage(context)
263
264    elif drawingType == UIHandling.kHICommandColorImageFromCallbacks:
265            doRawImageFileWithCallbacks(context)
266
267    elif drawingType == UIHandling.kHICommandGrayRamp:
268            Images.doGrayRamp(context)
269
270    elif drawingType == UIHandling.kHICommandDrawWithCGImageSource:
271            doDrawImageWithCGImageSource(context)
272
273    elif drawingType == UIHandling.kHICommandDrawWithCGImageSourceIncremental:
274            doIncrementalImage(context)
275
276    elif drawingType == UIHandling.kHICommandDrawWithQuickTime:
277            doQTImage(context)
278
279    elif drawingType == UIHandling.kHICommandSubstituteImageProfile:
280            doJPEGDocumentWithMultipleProfiles(context)
281
282    elif drawingType == UIHandling.kHICommandDoSubImage:
283            Images.doColorRampSubImage(context)
284
285    elif drawingType == UIHandling.kHICommandExportWithQuickTime:
286            Images.exportColorRampImageWithQT(context)
287
288    elif drawingType == UIHandling.kHICommandMaskTurkeyImage:
289            ImageMasking.doOneBitMaskImages(context)
290
291    elif drawingType == UIHandling.kHICommandImageMaskedWithMask:
292            doMaskImageWithMask(context)
293
294    elif drawingType == UIHandling.kHICommandImageMaskedWithGrayImage:
295            doMaskImageWithGrayImage(context)
296
297    elif drawingType == UIHandling.kHICommandMaskImageWithColor:
298            doImageMaskedWithColor(context)
299
300    elif drawingType == UIHandling.kHICommandClipToMask:
301            doClipMask(context)
302
303    elif drawingType == UIHandling.kHICommandExportWithCGImageDestination:
304            exportImageMaskedWithImage(context)
305
306    elif drawingType == UIHandling.kHICommandSimpleCGLayer:
307            BitmapContext.doSimpleCGLayer(context)
308
309    elif drawingType == UIHandling.kHICommandAlphaOnlyContext:
310            BitmapContext.doAlphaOnlyContext(context)
311
312    elif drawingType == UIHandling.kHICommandDrawNoOffScreenImage:
313            tilePDFDocument(context, noOffScreen)
314
315    elif drawingType == UIHandling.kHICommandDrawOffScreenImage:
316            tilePDFDocument(context, bitmapOffScreen)
317
318    elif drawingType == UIHandling.kHICommandDrawWithLayer:
319            tilePDFDocument(context, layerOffScreen)
320
321    elif drawingType == UIHandling.kHICommandQuartzRomanText:
322            QuartzTextDrawing.drawQuartzRomanText(context)
323
324    elif drawingType == UIHandling.kHICommandQuartzTextModes:
325            QuartzTextDrawing.drawQuartzTextWithTextModes(context)
326
327    elif drawingType == UIHandling.kHICommandQuartzTextMatrix:
328            QuartzTextDrawing.drawQuartzTextWithTextMatrix(context)
329
330    elif drawingType == UIHandling.kHICommandSimplePattern:
331            PatternDrawing.doRedBlackCheckerboard(context)
332
333    elif drawingType == UIHandling.kHICommandPatternPhase:
334            PatternDrawing.doPatternPhase(context)
335
336    elif drawingType == UIHandling.kHICommandPatternMatrix:
337            PatternDrawing.doPatternMatrix(context)
338
339    elif drawingType == UIHandling.kHICommandUncoloredPattern:
340            PatternDrawing.doStencilPattern(context)
341
342    elif drawingType == UIHandling.kHICommandDrawWithPDFPattern:
343            callPDFDrawProc(context, PatternDrawing.drawWithPDFPattern, kCatPDF)
344
345    elif drawingType == UIHandling.kHICommandSimpleShadow:
346            ShadowsAndTransparencyLayers.drawSimpleShadow(context)
347
348    elif drawingType == UIHandling.kHICommandShadowScaling:
349            ShadowsAndTransparencyLayers.doShadowScaling(context)
350
351    elif drawingType == UIHandling.kHICommandShadowProblems:
352            ShadowsAndTransparencyLayers.showComplexShadowIssues(context)
353
354    elif drawingType == UIHandling.kHICommandComplexShadow:
355            ShadowsAndTransparencyLayers.showComplexShadow(context)
356
357    elif drawingType == UIHandling.kHICommandMultipleShapeComposite:
358            ShadowsAndTransparencyLayers.doLayerCompositing(context)
359
360    elif drawingType == UIHandling.kHICommandFillAndStrokeWithShadow:
361            ShadowsAndTransparencyLayers.drawFillAndStrokeWithShadow(context)
362
363    elif drawingType == UIHandling.kHICommandPDFDocumentShadow:
364            callPDFDrawProc(context, ShadowsAndTransparencyLayers.shadowPDFDocument, kCatPDF)
365
366    elif drawingType == UIHandling.kHICommandSimpleAxialShading:
367            Shadings.doSimpleAxialShading(context)
368
369    elif drawingType == UIHandling.kHICommandExampleAxialShadings:
370            Shadings.doExampleAxialShading(context)
371
372    elif drawingType == UIHandling.kHICommandSimpleRadialShading:
373            Shadings.doSimpleRadialShading(context)
374
375    elif drawingType == UIHandling.kHICommandExampleRadialShadings:
376            Shadings.doExampleRadialShadings(context)
377
378    elif drawingType == UIHandling.kHICommandEllipseShading:
379            Shadings.doEllipseShading(context)
380
381    elif drawingType == UIHandling.kHICommandDoCompatibleEPS:
382            doCompatibleEPSDrawing(context)
383